﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.AppCode.Lib;

namespace mojoPortal.Web.RS.Modules.MLS.Controls
{
    public partial class PublicMLSListControl : RSBaseUserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            }
        }

        protected void gvMLS_Edit(object sender, CommandEventArgs e)
        {
            int propid = Convert.ToInt32(e.CommandArgument.ToString());
            if (propid > 0)
                Response.Redirect("RSPublicMLS.aspx?action=edit&item=" + propid);
        }
        protected void lnkMapIt_Click(object sender, CommandEventArgs e)
        {
            int mlsid = Convert.ToInt32(e.CommandArgument.ToString());
            if (mlsid > 0)
                ucModalViewMapControl.ShowMapDataByMLSID(mlsid);
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("RSPublicMLS.aspx?action=add");
        }

        protected void ddlMLSType_OnSelectedIndexChange(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int mlstype = int.Parse(ddlMLSType.SelectedValue);
            LoadMLSList(userIdentity.RSCompanyId, mlstype);
        }

        public bool LoadMLSList(int companyid = 0, int msltype = 0)
        {
            if (companyid > 0)
            {
                RSDataLookupLoader.LoadTypeDDL(companyid, "MLS_CAT", ddlMLSType);
            }
            else
            {
                RSDataLookupLoader.LoadPublicTypeDDL("MLS_CAT", ddlMLSType);
            }
            bool bHasValue = true;
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            RSUserIdentity userIdentity = GetRSUserIdentity();
            String sql = "exec rs_uspGetPublicMLS @MLSType";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ReadyServiceConnectionStringMain"].ToString());
            myCommand = new SqlCommand(sql, myConnection);
            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@MLSType";
            param.Value = msltype;
            myCommand.Parameters.Add(param);

            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            gvMLSList.DataSource = ds;
            gvMLSList.DataBind();
            myConnection.Close();

            if (gvMLSList.Rows.Count == 0)
            {
                bHasValue = false;
                return bHasValue;
            }
            return bHasValue;
        }
        
        //private string ConvertSortDirectionToSql(SortDirection sortDirection)
        //{
        //    string newSortDirection = String.Empty;

        //    switch (sortDirection)
        //    {
        //        case SortDirection.Ascending:
        //            newSortDirection = "ASC";
        //            break;
        //        case SortDirection.Descending:
        //            newSortDirection = "DESC";
        //            break;
        //    }
        //    return newSortDirection;
        //}

        protected void gvMLSList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvMLSList.PageIndex = e.NewPageIndex;
            gvMLSList.DataBind();
        }

        //protected void gvMLSList_Sorting(object sender, GridViewSortEventArgs e)
        //{
        //    DataTable dataTable = Session["DATATABLE"] as DataTable;
        //    if (dataTable != null)
        //    {
        //        DataView dataView = new DataView(dataTable);
        //        dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

        //        gvMLSList.DataSource = dataView;
        //        gvMLSList.DataBind();
        //    }
        //}

    }
}